package com.mall.server.controller.yxcontroller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.FlashSessionService;
import com.tanhua.model.domain.SmsFlashPromotionSession;
import com.tanhua.model.vo.SmsFlashPromotionSessionDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/flashSession")
public class FlashSessionController {

    @Autowired
    private FlashSessionService flashSessionService;

    /**
     * 获取秒杀时间段
     * @return
     */
    @GetMapping("/list")
    public R flashSessionList(){
        List<SmsFlashPromotionSession> list =flashSessionService.flashSessionList();
        return R.success(list);
    }

    /**
     * 修改秒杀时间段是否启用
     */
    @PostMapping("/update/status/{id}")
    public R updateStatus(@PathVariable("id")Long id,Integer status){
        if (id == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        flashSessionService.updateStatus(id, status);
        return R.success("");
    }

    /**
     * 添加秒杀时间段
     * @param smsFlashPromotionSession
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody SmsFlashPromotionSession smsFlashPromotionSession){
        if (smsFlashPromotionSession == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        flashSessionService.create(smsFlashPromotionSession);
        return R.success("");
    }

    /**
     * 修改秒杀时间段
     * @param id
     * @param smsFlashPromotionSession
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@PathVariable("id")Long id,@RequestBody SmsFlashPromotionSession smsFlashPromotionSession){

        if (id == null  || smsFlashPromotionSession ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }

        flashSessionService.update(smsFlashPromotionSession);
        return R.success("");
    }

    /**
     * 删除秒杀时间
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id")Long id){
        if (id == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        flashSessionService.delete(id);
        return R.success("");
    }


    //前端有问题,查询所有的时间进行返回
    /**
     * 根据flashPromotionId 查询sms_flash_promotion_product_relation 得到 sms_flash_promotion_session 集合返回
     * @param flashPromotionId
     * @return
     */
    @GetMapping("/selectList")
    public R findFlashPromotionIdList (Long flashPromotionId){
        List<SmsFlashPromotionSessionDetail> list =flashSessionService.findFlashPromotionIdList(flashPromotionId);
        return R.success(list);
    }

}
